package Stack;

public class conversion {
    public static String base_conversion(int number,int conversion){
        //提供一个数组用于寻找相应的要放入栈中的字符
        String[] Hex={"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
        SeqStack<String> stack = new SeqStack<String>();
        String str = number+"转换成"+conversion+"进制后为";
        if (number>0||conversion<2) {
            while (Math.abs(number) / conversion != 0) {
                stack.push(Hex[Math.abs(number) % conversion]);
                number = number / conversion;
            }
            stack.push(Hex[Math.abs(number) % conversion]);
        }else {
            while (Math.abs(number) / 2 != 0) {
                stack.push(Hex[Math.abs(number) % 2]);
                number = number / 2;
            }
            stack.push(Hex[Math.abs(number) % 2]);
        }
        //对于二进制负数的控制
        if (number<0){
            while (stack.size()<31){
                stack.push("0");
            }
            SeqStack<String> temp = new SeqStack<String>();
            //取反
            while (!stack.isEmpty()){
                temp.push(stack.pop().equals("1")?"0":"1");
            }

            //补码
            //如果最后一位是1则加1的时候需要不断往前挪动直到找到该位为0
            if (temp.pop().equals("1")){
                int count=0;
                while (!temp.pop().equals("0")){
                    count++;
                }
                for (int i=0;i<=count;i++){
                    stack.push("0");
                }
                temp.push("1");
                while (!temp.isEmpty()){
                    stack.push(temp.pop());
                }
            }else {
                stack.push("1");
                while (!temp.isEmpty()){
                    stack.push(temp.pop());
                }
            }
        }

        //定义除了二进制以外的其他进制的负数需要转换存放的栈
        if (conversion>2&&number<0){
            SeqStack<Integer> seqStack = new SeqStack<Integer>();
            while (!stack.isEmpty()){
                seqStack.push(Integer.valueOf(stack.pop()));
            }
            int count=1;
            int temp = 0;
            while (!seqStack.isEmpty()){
                int con=conversion==8?4:5;
                if (count%con==0){
                    stack.push(Hex[temp]);
                    count=1;
                    temp =0;
                }
                if (!seqStack.isEmpty()){
                    temp= (int) (temp+Math.pow(2,count-1)*seqStack.pop());
                }
                count++;
                if (seqStack.isEmpty())stack.push(Hex[temp]);
            }
        }
        while (!stack.isEmpty()){
            str+=stack.pop();
        }
        return str;
    }

    public static void main(String[] args) {
        //第一个参数为要转换的数字串，第二个参数为要转换的进制
        System.out.println(base_conversion(24,2));
        System.out.println(base_conversion(24,8));
        System.out.println(base_conversion(24,16));
        System.out.println(base_conversion(-24, 2));
        System.out.println(base_conversion(-24, 8));
        System.out.println(base_conversion(-24, 16));
    }
}